<?php

namespace App\Common;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class Download
{
    public static function downloadImg($url, $savaFilePath)
    {
        try {
            $client = new Client(['verify' => false]);
            $client->get($url, ['save_to' => $savaFilePath]);
            return file_exists($savaFilePath);
        } catch (\Exception $e) {
            return false;

        }
    }

    public static function downloadVedio($url, $savaPath)
    {

        $ts_content = file_get_contents($url);
        $ts_content = explode(',', $ts_content);

        $ts_file = array();
        foreach ($ts_content as $key => $value) {
            if ($key == 0) continue;
            $value = trim($value);
            $ts_file[] = substr($value, 0, strpos($value, '.ts') + 60);
        }

        $url_prefix = substr($url, 0, strpos($url, '.m3u8'));
        $url_prefix = substr($url, 0, strrpos($url, '/') + 1);
        $file_content = '';

        foreach ($ts_file as $key => $value) {
            $file_content .= file_get_contents($url_prefix . $value);
        }

        file_put_contents('tmp_out.ts', $file_content);
// FFMPEG_PATH 是你自己解压ffmpeg的bin路径，例如我的是F:/ffmpeg/bin/
        exec("ffmpeg -i tmp_out.ts {$savaPath}.mp4");
    }

    public static function getFile($url, $save_dir = '', $filename = '', $type = 0)
    {
        if (trim($url) == '') {
            return false;
        }
        if (trim($save_dir) == '') {
            $save_dir = './';
        }
        if (0 !== strrpos($save_dir, DS)) {
            $save_dir .= DS;
        }
        //创建保存目录
        if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
            return false;
        }
        //获取远程文件所采用的方法
        if ($type) {
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $content = curl_exec($ch);
            curl_close($ch);
        } else {
            ob_start();
            readfile($url);
            $content = ob_get_contents();
            ob_end_clean();
        }
        $size = strlen($content);
        //文件大小
        $fp2 = @fopen($save_dir . $filename, 'a');
        fwrite($fp2, $content);
        fclose($fp2);
        unset($content, $url);
        $res['code'] = 200;
        $res['fild_name'] = $filename;
        return $res;
    }


    public static function get_redirect_url($url, $referer = "", $timeout = 10)
    {
        $redirect_url = false;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($ch, CURLOPT_NOBODY, TRUE);//不返回请求体内容
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);//允许请求的链接跳转
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Accept: */*',
            'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
            'Connection: Keep-Alive'));
        if ($referer) {
            curl_setopt($ch, CURLOPT_REFERER, $referer);//设置referer
        }
        $content = curl_exec($ch);
        if (!curl_errno($ch)) {
            $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);//获取最终请求的url地址
        }
        return $redirect_url;
    }

    public static function testDowlowd($url, $file)
    {
        set_time_limit(0);

        $hostfile = fopen($url, 'r');
        $fh = fopen($file, 'wb');

        while (!feof($hostfile)) {
            $output = fread($hostfile, 8192);
            fwrite($fh, $output);
        }

        fclose($hostfile);
        fclose($fh);
    }
}